package com.voto.sunflower.activity.me;

import retrofit.RetrofitError;
import retrofit.client.Response;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.voto.sunflower.R;
import com.voto.sunflower.SunflowerApplication;
import com.voto.sunflower.activity.BaseActivity;
import com.voto.sunflower.activity.me.UpdateMeCallback.UpdateMeListener;
import com.voto.sunflower.model.request.UpdateMe;
import com.voto.sunflower.model.response.UserResponse;
import com.voto.sunflower.retrofit.ClientHttpService;
import com.voto.sunflower.retrofit.RestError;

public class ModifyNameActivity extends BaseActivity implements OnClickListener {

	protected static final String TAG = "ModifyNameActivity";
	private EditText nameEditText;
	private UpdateMeCallback mUpdateMeCallback;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_modify_name);
		
		setCallback();
		initView();
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		
	}

	private void setCallback() {
		mUpdateMeCallback = new UpdateMeCallback();
		mUpdateMeCallback.setListener(new UpdateMeListener() {
			
			@Override
			public void sucess(UserResponse arg0, Response arg1) {
				Log.d(TAG , "update name sucess------>");
				InputMethodManager imm = (InputMethodManager) ModifyNameActivity.this
						.getSystemService(Context.INPUT_METHOD_SERVICE);
				imm.hideSoftInputFromWindow(nameEditText.getWindowToken(), 0);
				
				SunflowerApplication.setmUser(arg0.getUser());
				finish();
			}
			
			@Override
			public void failure(RetrofitError error) {
				Log.d(TAG , "update name fail------>");
				if (error.getResponse() != null) {
					int statusCode = error.getResponse().getStatus();
					if (statusCode == 500 || statusCode == 501) {
						final RestError body = (RestError) error.getBodyAs(RestError.class);
						Toast.makeText(ModifyNameActivity.this, body.errorDetails, Toast.LENGTH_SHORT).show();
					} else if (statusCode == 408) {
						Toast.makeText(ModifyNameActivity.this, "连接超时", Toast.LENGTH_SHORT).show();
					} else {
						Toast.makeText(ModifyNameActivity.this, error.getMessage(), Toast.LENGTH_SHORT).show();
					}
				}
			}
		});
	}

	private void initView() {
		TextView back = (TextView) findViewById(R.id.back);
		TextView title = (TextView) findViewById(R.id.title);
		TextView other = (TextView) findViewById(R.id.other);
		nameEditText = (EditText) findViewById(R.id.name_edit);

		String name = getIntent().getExtras().getString("userName");
		nameEditText.setText(name);
		nameEditText.setSelection(name.length());// 设置光标位置到文字右边

		Resources rs = getResources();
		back.setOnClickListener(this);
		title.setText(rs.getString(R.string.update_name_title));
		other.setText(getResources().getString(R.string.save));
		other.setVisibility(View.VISIBLE);
		other.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.back:
			InputMethodManager imm = (InputMethodManager) this
					.getSystemService(Context.INPUT_METHOD_SERVICE);
			imm.hideSoftInputFromWindow(nameEditText.getWindowToken(), 0);
			finish();
			break;
		case R.id.other:
			updateUserInfo();
			break;
		default:
			break;
		}

	}

	/**
	 * 失去焦点
	 */
	public void loseFocus(View view) {
		view.setFocusable(true);
		view.setFocusableInTouchMode(true);
		view.requestFocus();
	}

	protected void updateUserInfo() {
		UpdateMe update = new UpdateMe();
		String name = nameEditText.getText().toString();
		if(TextUtils.isEmpty(name)){
			Toast.makeText(this, "名字不能为空", Toast.LENGTH_SHORT).show();
		}else{
			update.setName(name);
			ClientHttpService.getMeService().updateMe(update, mUpdateMeCallback);
		}
	}
}
